<template xmlns:v-slot="http://www.w3.org/1999/XSL/Transform">
  <div>
    <page-dialog
      :title="dialogInfo.title"
      v-model:visible="shows"
      :width="dialogInfo.width"
      :bt-loading="dialogInfo.btLoading"
      :bt-list="dialogInfo.btList"
    >
      <div class="layui-row">
        <!-- 条件栏 -->
        <new-page-filter
          v-model:query="filterInfo.query"
          :filter-list="filterInfo.list"
          :filter-btn="filterInfo.searchBtn"
        >
        </new-page-filter>
        <dialog-table
          :refresh="tableInfo.refresh"
          :init-curpage="tableInfo.initCurpage"
          v-model:data="tableInfo.data"
          :api="tableInfo.api"
          ref="productTable"
          :ref-obj="tableInfo.ref"
          class="productTable"
          :isCompareChecked="true"
          :reserveSelection="true"
          :listenHeight="false"
          v-model:query="filterInfo.query"
          :field-list="tableInfo.fieldList"
          :handle="tableInfo.handle"
          :check-box="checkBox"
          :isRowClickSelect="true"
          @handleEvent="handleEvent"
        >
        </dialog-table>
      </div>
    </page-dialog>
  </div>
</template>

<script setup name="SelectOrder">
import {
  getPurchApplicationPage,
  createApplicationSummaryMergeInsert,
  createApplicationSummarySaveAndSubmit
} from '@/api/purch'

const emits = defineEmits()
const { proxy } = getCurrentInstance()
const props = defineProps({
  checkBox: {
    type: Boolean,
    default: true,
  },
  api: {
    type: Function,
    default: getPurchApplicationPage,
  },
  showDialog: {
    type: Boolean,
  },
  query: {
    type: Object,
    default: function () {
      return {}
    },
  },
  defaultSelect: {
    type: Array,
    default: function () {
      return []
    },
  },
})
// 过滤相关配置
const filterInfo = reactive({
  query: {
    ...proxy.query,
    sort: 'create_time desc',
    statusMult: '2,3,13',
    deptAuth: 'deptId',
    purchProductType: '1',
  },
  list: [
    {
      componentType: 'input',
      label: '单号',
      value: 'orderNoLike',
      event: search,
    },
    {
      label: '状态',
      value: 'status',
      componentType: 'select-model',
      code: 'enable_summary_status',
      clearable: false,
      event: search,
    },
  ],
  searchBtn: [{ type: 'button', label: '搜索', event: search }],
})

// 表格相关
const tableInfo = reactive({
  refresh: 1,
  initCurpage: 1,
  height: '70%',
  ref: {},
  api: props.api,
  data: [],
  selections: [],
  fieldList: [
    { label: '申购单号', value: 'orderNo' },
    { label: '状态', value: 'statusValue' },
    { label: '是否临采', value: 'temporaryPurchValue' },
    { label: '部门', value: 'deptName' },
    { label: '创建人', value: 'createUserName' },
    { label: '创建时间', value: 'createTime' },
  ],
})
// 弹窗相关
const dialogInfo = reactive({
  title: '选择申购单',
  btLoading: false,
  width: '75%',
  btList: [
    { label: '关闭', event: handleClose, show: true },
    { label: '保存并提交', event: saveAndSubmit, show: proxy.$permission('applicationSummary:saveAndSubmit')},
    {
      label: '保存',
      type: 'primary',
      disabled: false,
      event: save,
      show: true,
    },
  ],
})
const shows = computed({
  get() {
    return props.showDialog
  },
  set(newValue) {
    emits('update:showDialog', newValue) // 更新父组件visible
  },
})
getList()
onMounted(() => {
  setTimeout(() => {
    getList()
  }, 100)
})
function getList() {
  tableInfo.initCurpage = Math.random()
  tableInfo.refresh = Math.random()
}
function search() {
  tableInfo.initCurpage = Math.random()
  tableInfo.refresh = Math.random()
}
function saveAndSubmit() {
  save(true)
}
function save(isSaveAndSubmit) {
  if (tableInfo.selections.length) {
    let ids = tableInfo.selections.map((a) => a.id)
    if (props.query.status) {
      handleClose()
      emits('callback', ids)
    } else {
      let api = isSaveAndSubmit === true ? createApplicationSummarySaveAndSubmit : createApplicationSummaryMergeInsert
      api(ids).then((res) => {
        if (res && res.success) {
          handleClose()
          emits('callback')
        }
      })
    }
  } else {
    proxy.$message({
      type: 'warning',
      duration: 3500,
      showClose: true,
      message: '请选择订单',
    })
  }
}
function handleEvent(event, data) {
  switch (event) {
    case 'tableCheck':
      tableInfo.selections = [...data]
      break
  }
}
// 关闭弹窗前的回调
function handleClose() {
  shows.value = false
  emits('reset')
}
</script>

<style scoped></style>
